package com.vavr.checkincms.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.vavr.checkincms.common.constant.CommonConstants;
import com.vavr.checkincms.common.domain.ResultEnum;
import com.vavr.checkincms.common.exception.BaseException;
import com.vavr.checkincms.constant.CheckinConstants;
import com.vavr.checkincms.convert.MeetingInfoConvert;
import com.vavr.checkincms.domain.bo.MeetingInfoBO;
import com.vavr.checkincms.domain.data.MeetingInfoDO;
import com.vavr.checkincms.domain.data.SystemUserInfoDO;
import com.vavr.checkincms.domain.dto.MeetingConfigDTO;
import com.vavr.checkincms.domain.dto.MeetingInfoDTO;
import com.vavr.checkincms.mapper.MeetingInfoMapper;
import com.vavr.checkincms.mapper.SystemLoginMapper;
import com.vavr.checkincms.util.CheckinRequestUtils;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * MeetingInfoServiceImpl
 *
 * @since 2024.08
 */
@Service
public class MeetingInfoServiceImpl implements MeetingInfoService {
    /**
     * meetingInfoMapper
     */
    private final MeetingInfoMapper meetingInfoMapper;

    /**
     * systemLoginMapper
     */
    private final SystemLoginMapper systemLoginMapper;

    public MeetingInfoServiceImpl(MeetingInfoMapper meetingInfoMapper, SystemLoginMapper systemLoginMapper) {
        this.meetingInfoMapper = meetingInfoMapper;
        this.systemLoginMapper = systemLoginMapper;
    }

    @Override
    public int addMeetingInfo(MeetingInfoDO meetingInfo) {
        return meetingInfoMapper.insertMeetingInfo(meetingInfo);
    }

    @Override
    public MeetingInfoDTO getMeetingInfoById(Integer meetingId) {
        QueryWrapper<MeetingInfoDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", 0);
        queryWrapper.eq("meeting_id", meetingId);
        MeetingInfoDO meetingInfoDO = meetingInfoMapper.selectOne(queryWrapper);
        if (meetingInfoDO != null) {
            return MeetingInfoConvert.INSTANCE.doToDto(meetingInfoDO);
        }
        return null;
    }

    @Override
    public List<MeetingInfoDO> getMeetingInfoList() {
        QueryWrapper<MeetingInfoDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", 0);
        return meetingInfoMapper.selectList(queryWrapper);
    }

    @Override
    public List<MeetingConfigDTO> getMeetingConfig(String meetingId) {
        return meetingInfoMapper.queryMeetingInfo(meetingId);
    }

    @Override
    public List<MeetingConfigDTO> queryMeetingConfigByConfigId(String configId) {
        return meetingInfoMapper.queryMeetingConfigByConfigId(configId);
    }

    @Override
    public List<MeetingInfoDTO> getMeetingList(MeetingInfoBO bo) {
        // 获取当前登录角色信息
        Optional<SystemUserInfoDO> systemUserInfoOptional = CheckinRequestUtils.getUserFromRequestAttributes()
                .map(v -> systemLoginMapper.selectById(v.getOperatorId()));
        if (systemUserInfoOptional.isEmpty()) {
            throw new BaseException(ResultEnum.NOT_CHECKIN);
        }
        SystemUserInfoDO userInfoDO = systemUserInfoOptional.get();
        Map<String, MeetingInfoDTO> meetingInfoMap = new HashMap<>();
        if (StrUtil.equals(CommonConstants.IS_ADMIN, userInfoDO.getOperatorLevel())) {
            // 查询所有会议信息
            meetingInfoMapper.queryAllMeeting(bo.getMeetingId()).forEach(item -> {
                sortMeetingConfig(item, meetingInfoMap);
            });
        }
        else {
            meetingInfoMapper.queryMeetingByOperator(bo.getMeetingId(),userInfoDO.getOperatorId()).forEach(item -> {
                sortMeetingConfig(item, meetingInfoMap);
            });
        }
         return meetingInfoMap.values().stream().toList();
    }

    @Override
    public void getMeetingUserInfo(MeetingInfoBO bo, HttpServletResponse response, HttpServletRequest request) {

    }

    private static void sortMeetingConfig(MeetingConfigDTO item, Map<String, MeetingInfoDTO> meetingInfoMap) {
        if (!StrUtil.equalsAny(item.getConfigId(), CheckinConstants.MEETING_NAME, CheckinConstants.START_TIME,
                CheckinConstants.END_TIME)) {
            return;
        }
        MeetingInfoDTO meetingInfoDTO = meetingInfoMap.get(item.getMeetingId());
        if (meetingInfoDTO == null) {
            meetingInfoDTO = new MeetingInfoDTO();
            meetingInfoDTO.setMeetingId(item.getMeetingId());
            meetingInfoDTO.setConfigs(new ArrayList<>());
        }
        meetingInfoDTO.getConfigs().add(item);

        if (StrUtil.equals(item.getConfigId(), CheckinConstants.MEETING_NAME)) {
            meetingInfoDTO.setMeetingName(item.getConfigValue());
        }
        if (StrUtil.equals(item.getConfigId(), CheckinConstants.START_TIME)) {
            meetingInfoDTO.setMeetingName(item.getConfigValue());
        }
        if (StrUtil.equals(item.getConfigId(), CheckinConstants.END_TIME)) {
            meetingInfoDTO.setMeetingName(item.getConfigValue());
        }
        meetingInfoMap.put(item.getMeetingId(), meetingInfoDTO);
    }
}
